/**
 * Created by IntelliJ IDEA.
 * User: user
 * Date: 10.10.12
 * Time: 19:17
 * To change this template use File | Settings | File Templates.
 */
package zakladki.AS3.country {
import avmplus.constantXml;

import mx.collections.ArrayCollection;
import mx.collections.XMLListCollection;
import mx.controls.Alert;

public class GDPEnergyDataProvider {
    public function GDPEnergyDataProvider() {
    }


    public function getCountryEnergyGDPData(continentXML:XML):ArrayCollection {
        var result:ArrayCollection = new ArrayCollection();
        if(continentXML != null){
            var xmlGDPCollection:XMLListCollection = null;
            var xmlEnergyCollection:XMLListCollection = null;
            var countryNames:XMLListCollection = null;
            var showPLotFlagCollection:XMLListCollection = null;
            var includeInGDPCollection:XMLListCollection = null;

            try {
                xmlGDPCollection = new XMLListCollection(continentXML.Continent.Country.PKBPerCapita);
            } catch(e:Error) {
                Alert.show("Error getting gdp collection: " + e.message);
            }

            try {
                xmlEnergyCollection = new XMLListCollection(continentXML.Continent.Country.kWh_rok);
            } catch(e:Error) {
                Alert.show("Error getting energy collection: " + e.message);
            }

            try{
                countryNames = new XMLListCollection(continentXML.Continent.Country.Name);
            }catch(e:Error){
                Alert.show("Error getting country names collection: " + e.message);
            }

            try{
                showPLotFlagCollection = new XMLListCollection(continentXML.Continent.Country.ShowPlotFlag);
            }catch(e:Error){
                Alert.show("Error getting country names collection: " + e.message);
            }

            try{
               includeInGDPCollection = new XMLListCollection(continentXML.Continent.Country.IncludeInGDP);
            }catch(e:Error){
                Alert.show("Error getting includeInGDP collection: " + e.message);
            }

            var continentRegion:String = continentXML.Continent.Region;

            if (xmlEnergyCollection != null && xmlGDPCollection != null) {
                if (xmlGDPCollection.length > 0 && xmlEnergyCollection.length > 0 && xmlGDPCollection.length == xmlEnergyCollection.length) {
                    for (var i:int = 0; i < xmlGDPCollection.length; i++) {
                        var gdp:Object = xmlGDPCollection.getItemAt(i);
                        var energy:Object = xmlEnergyCollection.getItemAt(i);
                        var includeInGDP:Boolean = new Number(includeInGDPCollection.getItemAt(i).toString()) == 1;
                        if (includeInGDP) {
                            var num:Number = new Number(energy.toString());
                            num = num / 365;
                            energy = num.toString();

                            var showFlag:Boolean = new Number(showPLotFlagCollection.getItemAt(i).toString()) == 1;
                            var ctrName:String = countryNames.getItemAt(i).toString();

                            var item:Object = {gdp:gdp,energy:energy,flag:showFlag,countryName:ctrName,region:continentRegion};
                            result.addItem(item);
                        }
                    }
                }
            }
        }
        return result;
    }
}
}
